#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 1e4 + 10;

struct Person{
    string name;
    int h;

    bool operator < (const Person & p1) const {
        if (h != p1.h) return h > p1.h;
        return name < p1.name;
    }
}p[N];

int n, m;
vector<vector<string>> res;

inline void solution() {
    cin >> n >> m;
    for (int i = 0; i < n; i ++ ) 
        cin >> p[i].name >> p[i].h;
    sort(p, p + n);

    int row = m, col, last_col, idx = 0;
    col = n / m;
    last_col = n - n / m * (m - 1);
    vector<string> path;
    for (int i = 0; i < last_col; i ++) {
        if (i % 2) {
            path.insert(path.begin(), p[idx ++ ].name);
        } else {
            path.push_back(p[idx ++ ].name);
        }
    }
    res.push_back(path);
    for (int i = 1; i < row; i ++ ) {
        path.clear();
        for (int j = 0; j < col; j ++ ) {
            if (j % 2) {
                path.insert(path.begin(), p[idx ++ ].name);
            } else {
                path.push_back(p[idx ++ ].name);
            }
        }
        res.push_back(path);
    }

    for (auto & r : res) {
        for (int i = 0; i < r.size(); i ++ ) {
            if (i != 0) cout << " ";
            cout << r[i]; 
        }
        cout << endl;
    }
}

int main() {
    freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}